package com.ruoyi.project.pc.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.project.pc.domain.Advertisement;
import com.ruoyi.project.pc.mapper.AdvertisementMapper;
import com.ruoyi.project.pc.service.IAdvertisementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 广告、小程序首页图片Service业务层处理
 * 
 * @author luaoran
 * @date 2023-06-05
 */
@Service
public class AdvertisementServiceImpl implements IAdvertisementService
{
    @Autowired
    private AdvertisementMapper advertisementMapper;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询广告、小程序首页图片
     * 
     * @param id 广告、小程序首页图片ID
     * @return 广告、小程序首页图片
     */
    @Override
    public Advertisement selectAdvertisementById(Long id)
    {
        return advertisementMapper.selectAdvertisementById(id);
    }

    /**
     * 查询广告、小程序首页图片列表
     * 
     * @param advertisement 广告、小程序首页图片
     * @return 广告、小程序首页图片
     */
    @Override
    public List<Advertisement> selectAdvertisementList(Advertisement advertisement)
    {
        return advertisementMapper.selectAdvertisementList(advertisement);
    }

    /**
     * 新增广告、小程序首页图片
     * 
     * @param advertisement 广告、小程序首页图片
     * @return 结果
     */
    @Override
    public int insertAdvertisement(Advertisement advertisement)
    {
        advertisement.setCreateTime(DateUtils.getNowDate());
        Integer maxNumber = advertisementMapper.getNumber(advertisement.getType());
        int number = 1;
        if (maxNumber == null) {
            advertisement.setNumber(number);
        } else {
            advertisement.setNumber(++maxNumber);
        }
        return advertisementMapper.insertAdvertisement(advertisement);
    }

    /**
     * 修改广告、小程序首页图片
     * 
     * @param advertisement 广告、小程序首页图片
     * @return 结果
     */
    @Override
    public int updateAdvertisement(Advertisement advertisement, MultipartFile image) throws Exception
    {
        Long id = advertisement.getId();
        Assert.notNull(id,"主键ID不能为空");
        advertisement.setUpdateTime(DateUtils.getNowDate());
        if ("del".equals(advertisement.getUrl())) {
            advertisementMapper.delImage(id);
            advertisement.setUrl(null);
        }

        if (image != null && image.getSize() !=0) {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, image);
            String url = serverConfig.getUrl() + fileName;
            advertisement.setUrl(url);
        }

        return advertisementMapper.updateAdvertisement(advertisement);
    }

    /**
     * 批量删除广告、小程序首页图片
     * 
     * @param ids 需要删除的广告、小程序首页图片ID
     * @return 结果
     */
    @Override
    public int deleteAdvertisementByIds(Long[] ids)
    {
        return advertisementMapper.deleteAdvertisementByIds(ids);
    }

    /**
     * 删除广告、小程序首页图片信息
     * 
     * @param id 广告、小程序首页图片ID
     * @return 结果
     */
    @Override
    public int deleteAdvertisementById(Long id)
    {
        return advertisementMapper.deleteAdvertisementById(id);
    }

    @Override
    public int updateNumber(List<Advertisement> advertisementList) {
        int count = 0;
        if (CollectionUtils.isEmpty(advertisementList)) {
            return count;
        }
        for (Advertisement advertisement : advertisementList) {
            advertisementMapper.updateAdvertisement(advertisement);
            ++count;
        }
        return count;
    }

    @Override
    public int editNumber(Advertisement advertisement) {
        advertisement.setUpdateTime(DateUtils.getNowDate());
        return advertisementMapper.updateAdvertisement(advertisement);
    }
}
